package com.example.hrsystem.workflow.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.model.WorkflowTask;

import java.util.ArrayList;
import java.util.List;

/**
 * 工作流任务列表适配器
 * 用于在RecyclerView中显示工作流任务列表
 */
public class WorkflowTaskAdapter extends RecyclerView.Adapter<WorkflowTaskAdapter.TaskViewHolder> {
    private List<WorkflowTask> tasks = new ArrayList<>();
    private OnTaskClickListener listener;

    @NonNull
    @Override
    public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_workflow_task, parent, false);
        return new TaskViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
        WorkflowTask task = tasks.get(position);
        holder.bind(task);
    }

    @Override
    public int getItemCount() {
        return tasks.size();
    }

    public void setTasks(List<WorkflowTask> tasks) {
        this.tasks = tasks;
        notifyDataSetChanged();
    }

    public void setOnTaskClickListener(OnTaskClickListener listener) {
        this.listener = listener;
    }

    class TaskViewHolder extends RecyclerView.ViewHolder {
        private TextView nameTextView;
        private TextView descriptionTextView;
        private TextView statusTextView;
        private TextView createTimeTextView;

        public TaskViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTextView = itemView.findViewById(R.id.text_task_name);
            descriptionTextView = itemView.findViewById(R.id.text_task_description);
            statusTextView = itemView.findViewById(R.id.text_task_status);
            createTimeTextView = itemView.findViewById(R.id.text_create_time);

            // 设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (listener != null && position != RecyclerView.NO_POSITION) {
                        listener.onTaskClick(tasks.get(position));
                    }
                }
            });
        }

        public void bind(WorkflowTask task) {
            nameTextView.setText(task.getName());
            descriptionTextView.setText(task.getDescription());
            statusTextView.setText(task.getStatus());
            createTimeTextView.setText(task.getCreateTime().toString());
        }
    }

    public interface OnTaskClickListener {
        void onTaskClick(WorkflowTask task);
    }
}